package com.leezt.designpattern.creation.s02Factory.simple;

import com.leezt.designpattern.creation.s02Factory.common.RuleConfig;
import com.leezt.designpattern.creation.s02Factory.service.IRuleConfigParser;
import com.leezt.designpattern.creation.s02Factory.common.exceptions.InvalidRuleConfigException;

/*
* simple factory implementation
* */
public class RuleConfigSource {
    public RuleConfig load(String ruleConfigFilePath) throws InvalidRuleConfigException {
        String ruleConfigFileExtension = getFileExtension(ruleConfigFilePath);
        IRuleConfigParser parser = RuleConfigParserFactory.createParser(ruleConfigFileExtension);
        if (parser == null) {
            throw new InvalidRuleConfigException("Rule config file format is not supported: " + ruleConfigFilePath);
        }

        String configText = "";
        //从 ruleConfigFilePath 文件中读取配置文本到 configText 中
        return parser.parse(configText);
    }

    private String getFileExtension(String filePath) {
        //...解析文件名获取扩展名，比如rule.json，返回json
        return "json";
    }
}
